package com.neusoft.neuiotms.dcam.fixed.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.dto.ContractDTO;
import com.neusoft.neuiotms.dcam.fixed.service.ContractService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "合同管理")
@RestController
@RequestMapping(value = "/api/w/contracts")
public class ContractController {

    @Autowired
    private ContractService contractService;

    @ApiOperation("查询所有合同")
    @GetMapping
    public ResultListDTO<ContractDTO> index() {
        final List<ContractDTO> DTOs = this.contractService.findAll();
        final ResultListDTO<ContractDTO> result = ResultListDTO.success(DTOs);
        return result;
    }

    @ApiOperation("分页查询所有合同")
    @GetMapping("/p")
    public PageResultDTO<ContractDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.contractService.search(pageable, searchable));
    }

    @ApiOperation("创建合同")
    @PostMapping
    public ResultDTO<ContractDTO> create(@RequestBody final ContractDTO dto) {
        final ContractDTO result = this.contractService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("合同详情")
    @GetMapping("/{id}")
    public ResultDTO<ContractDTO> details(@PathVariable final Long id) {
        final ContractDTO dto = this.contractService.details(id);
        return ResultDTO.success(dto);
    }

    @ApiOperation("删除合同")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.contractService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改合同")
    @PutMapping("/{id}")
    public ResultDTO<ContractDTO> update(@PathVariable final Long id,
            @RequestBody final ContractDTO dto) {
        final ContractDTO result = this.contractService.update(id, dto);
        return ResultDTO.success(result);
    }

}
